package com.focusai.efairy.ui.adapter.base;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * 已选商品适配器
 * <p/>
 */
public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<BaseRecyclerViewHolder> {

    protected static final int TYPE_HEADER = 0;
    protected static final int TYPE_ITEM = 1;
    protected static final int TYPE_FOOTER = 2;

    protected Context mContext;
    protected List<T> mList;
    protected OnItemClickListener mOnItemClickListener;
    protected OnItemLongClickListener mOnItemLongClickListener;
    private int mClickPosition = -1;

    /**
     * 可以是空参数构造方法，但不能使用mContext对象
     */
    public BaseRecyclerAdapter() {

    }

    public BaseRecyclerAdapter(Context context) {
        this.mContext = context;
    }

    public void refreshData(List<T> list) {
        this.mList = list;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        mOnItemLongClickListener = onItemLongClickListener;
    }

    public void setClickPosition(int position) {
        mClickPosition = position;
    }

    public int getClickPosition() {
        return mClickPosition;
    }

    /**
     * 添加HeaderView、FooterView要重写该方法
     * getItemCount()
     * getItemViewType(int position)
     * getHeaderLayoutId()/getFooterLayoutId()
     */
    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public List<T> getData() {
        return mList;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEADER) {
            return getHeaderViewHolder(parent);
        } else if (viewType == TYPE_FOOTER) {
            return getFooterViewHolder(parent);
        } else {
            return getItemViewHolder(parent);
        }
    }

    /**
     * 添加HeaderView、FooterView要重写该方法
     * getItemCount()
     * getItemViewType(int position)
     * getHeaderLayoutId()/getFooterLayoutId()
     */
    @Override
    public int getItemViewType(int position) {
        return TYPE_ITEM;
    }

    @Override
    public void onBindViewHolder(BaseRecyclerViewHolder holder, final int position) {
        onBindBaseRecyclerViewHolder(holder, position);
    }

    /**
     * item view
     * @param parent
     * @return
     */
    private BaseRecyclerViewHolder getItemViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(getItemLayoutId(), parent, false);
        final BaseRecyclerViewHolder holder = new BaseRecyclerViewHolder(parent.getContext(), view);
        if (mOnItemClickListener != null) {
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(holder);
                }
            });
        }
        if (mOnItemLongClickListener != null) {
            view.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnItemLongClickListener.onItemLongClick(holder);
                    return true;
                }
            });
        }
        return holder;
    }

    /**
     * 头部 view
     * @param parent
     * @return
     */
    protected BaseRecyclerViewHolder getHeaderViewHolder(ViewGroup parent) {
        return new HeaderViewHolder(parent.getContext(), LayoutInflater.from(parent.getContext()).inflate(getHeaderLayoutId(), null, false));
    }

    /**
     * 底部 view
     * @param parent
     * @return
     */
    protected BaseRecyclerViewHolder getFooterViewHolder(ViewGroup parent) {
        return new FooterViewHolder(parent.getContext(), LayoutInflater.from(parent.getContext()).inflate(getFooterLayoutId(), null, false));
    }

    abstract protected int getItemLayoutId();

    /**
     * 添加HeaderView、FooterView要重写该方法
     * getItemCount()
     * getItemViewType(int position)
     * getHeaderLayoutId()/getFooterLayoutId()
     */
    protected int getHeaderLayoutId() {
        return 0;
    }

    /**
     * 添加HeaderView、FooterView要重写该方法
     * getItemCount()
     * getItemViewType(int position)
     * getHeaderLayoutId()/getFooterLayoutId()
     */
    protected int getFooterLayoutId() {
        return 0;
    }

    abstract protected void onBindBaseRecyclerViewHolder(BaseRecyclerViewHolder holder, int position);

    /**
     * item点击事件回调接口
     */
    public interface OnItemClickListener {
        void onItemClick(BaseRecyclerViewHolder holder);
    }

    /**
     * item长按事件回调接口
     */
    public interface OnItemLongClickListener {
        void onItemLongClick(BaseRecyclerViewHolder holder);
    }

    protected static class HeaderViewHolder extends BaseRecyclerViewHolder {
        public HeaderViewHolder(Context context, View view) {
            super(context, view);
        }
    }

    protected static class FooterViewHolder extends BaseRecyclerViewHolder {
        public FooterViewHolder(Context context, View view) {
            super(context, view);
        }
    }
}
